use crate::config::app_error::AppResult;
use crate::extension::index::index::indexer::DefaultIndexer;
use crate::extension::index::index::query::query::{BaseQuery, QueryIndexView};
use halo_model::{ExtensionOperator, GVK};
use ordermap::OrderSet;
use std::hash::Hash;

pub(crate) struct GreaterThan {
    pub field_name: String,
    pub value: String,
    pub is_field_ref: bool,
    pub or_equal: bool,
}

impl GreaterThan {
    pub fn new(field_name: String, value: String, or_equal: bool) -> Self {
        GreaterThan {
            field_name,
            value,
            is_field_ref: false,
            or_equal,
        }
    }

    pub fn new_all(field_name: String, value: String, or_equal: bool, is_field_ref: bool) -> Self {
        GreaterThan {
            field_name,
            value,
            is_field_ref,
            or_equal,
        }
    }
}

impl BaseQuery for GreaterThan {
    fn matches(
        &self,
        index_view: &QueryIndexView<DefaultIndexer>,
    ) -> AppResult<OrderSet<String>> {
        if self.is_field_ref {
            return index_view.find_matching_ids_with_greater_values(
                &self.field_name,
                &self.value,
                self.or_equal,
            );
        }
        index_view.find_ids_greater_than(&self.field_name, &self.value, self.or_equal)
    }
}
